//
//  HZApiTool.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/3/24.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "HZApiTool.h"

@implementation HZApiTool

+ (NSURLSessionDataTask *)getUrl:(NSString *)url
                          params:(NSDictionary *)params
                         success:(void (^)(id))success
                         failure:(void (^)(NSError *error))failure
{
    return [self getUrl:url params:params responseNoJson:NO success:success failure:failure];
}

+ (NSURLSessionDataTask *)getUrl:(NSString *)url
                          params:(NSDictionary *)params
                  responseNoJson:(BOOL)responseNoJson
                         success:(void (^)(id))success
                         failure:(void (^)(NSError *error))failure
{
    // 获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (responseNoJson) {
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    // 请求超时
    manager.requestSerializer.timeoutInterval = 30.0;
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/plain", nil];
    
    NSURLSessionDataTask *task = [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject == nil) {
            failure(nil);
            return;
        }
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
    return task;
}

+ (NSURLSessionDataTask *)postUrl:(NSString *)url
                           params:(NSDictionary *)params
                          success:(void (^)(id x))success
                          failure:(void (^)(NSError *error))failure
{
    return [self postUrl:url params:params responseNoJson:NO success:success failure:failure];
}

+ (NSURLSessionDataTask *)postUrl:(NSString *)url
                           params:(NSDictionary *)params
                         progress:(void (^)(id))progress
                          success:(void (^)(id x))success
                          failure:(void (^)(NSError *error))failure
{
    return [self postUrl:url params:params responseNoJson:NO progress:progress success:success failure:failure];
}

+ (NSURLSessionDataTask *)postUrl:(NSString *)url
                           params:(NSDictionary *)params
                   responseNoJson:(BOOL)responseNoJson
                          success:(void (^)(id x))success
                          failure:(void (^)(NSError *error))failure
{
    return [self postUrl:url params:params responseNoJson:responseNoJson progress:nil success:success failure:failure];
}

+ (NSURLSessionDataTask *)postUrl:(NSString *)url
                           params:(NSDictionary *)params
                   responseNoJson:(BOOL)responseNoJson
                         progress:(void (^)(id))progress
                          success:(void (^)(id x))success
                          failure:(void (^)(NSError *error))failure
{
    return [self postUrl:url params:params requestIsJson:NO responseNoJson:responseNoJson progress:progress success:success failure:failure];
}

+ (NSURLSessionDataTask *)postJsonUrl:(NSString *)url
                               params:(NSDictionary *)params
                              success:(void (^)(id x))success
                              failure:(void (^)(NSError *error))failure
{
    return [self postUrl:url params:params requestIsJson:YES responseNoJson:NO progress:nil success:success failure:failure];
}

+ (NSURLSessionDataTask *)postJsonUrl:(NSString *)url
                               params:(NSDictionary *)params
                             progress:(void (^)(id))progress
                              success:(void (^)(id x))success
                              failure:(void (^)(NSError *error))failure
{
    return [self postUrl:url params:params requestIsJson:YES responseNoJson:NO progress:progress success:success failure:failure];
}

+ (NSURLSessionDataTask *)postUrl:(NSString *)url
                           params:(NSDictionary *)params
                    requestIsJson:(BOOL)requestIsJson
                   responseNoJson:(BOOL)responseNoJson
                         progress:(void (^)(id))progress
                          success:(void (^)(id x))success
                          failure:(void (^)(NSError *error))failure
{
    // 获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if (responseNoJson) {
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    if (requestIsJson) {
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
    }
    manager.requestSerializer.timeoutInterval = 10.0;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/plain", nil];
    
//    manager.securityPolicy = [self customSecurityPolicy];
    
    
    NSURLSessionDataTask *task = [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
    return task;
}

//https
+ (AFSecurityPolicy *)customSecurityPolicy {
    // 先导入证书 证书由服务端生成，具体由服务端人员操作
//    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];//证书的路径
//    NSData *cerData = [NSData dataWithContentsOfFile:cerPath];
    
    // AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    // allowInvalidCertificates 是否允许无效证书（也就是自建的证书），默认为NO
    // 如果是需要验证自建证书，需要设置为YES
//    securityPolicy.allowInvalidCertificates = YES;
    
    //validatesDomainName 是否需要验证域名，默认为YES;
    //假如证书的域名与你请求的域名不一致，需把该项设置为NO；如设成NO的话，即服务器使用其他可信任机构颁发的证书，也可以建立连接，这个非常危险，建议打开。
    //置为NO，主要用于这种情况：客户端请求的是子域名，而证书上的是另外一个域名。因为SSL证书上的域名是独立的，假如证书上注册的域名是www.google.com，那么mail.google.com是无法验证通过的；当然，有钱可以注册通配符的域名*.google.com，但这个还是比较贵的。
    //如置为NO，建议自己添加对应域名的校验逻辑。
//    securityPolicy.validatesDomainName = NO;
//    securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:cerData, nil];
    
    return securityPolicy;
}


+ (NSURLSessionDataTask *)getNoCacheUrl:(NSString *)url
                                 params:(NSDictionary *)params
                                success:(void (^)(id))success
                                failure:(void (^)(NSError *error))failure
{
    // 获得请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 请求超时
    manager.requestSerializer.timeoutInterval = 30.0;
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",@"text/json",@"text/plain", nil];
    
    NSURLSessionDataTask *task = [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject == nil) {
            failure(nil);
            return;
        }
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
    
    return task;
}

@end
